<?php defined('SYSPATH') or die('No direct script access.');

	class Controller_RawSignal extends Controller{
		
		public function action_index(){
			$page = (isset($_GET['page']))? $_GET['page'] : 1;
			$limit = 10;
			
			$rawSignalService = Helper_Service::factory("rawsignal");
			$r_signals = $rawSignalService->getList($page, $limit);
			
			$rows = array();
			foreach($r_signals as $signal){
				array_push($rows, array(Helper_Date::mysqlToDate($signal->creationdate),$signal->filename,$signal->sampling_period->k_value,$signal->id));
			}
						
			$rawsignalslist = View::factory("components/rawsignalslist");
			$rawsignalslist->title = "Se&ntilde;ales Originales";
			//$rawsignalslist->columns = array("Fecha Incidencia","Archivo","Paciente","Per&iacute;odo Muestreo","Estado","Creado el");
			$rawsignalslist->columns = array("Fecha Alta","Archivo","Periodo de Muestreo");
			$rawsignalslist->rows = $rows;
			
			$links = Pagination::factory(array('items_per_page' => $limit,'query_string' => 'page','total_items' => $rawSignalService->count(),'view'=>'pagination/diastole-links'));
			
			Fire::log($links->render());
			
			$rawsignalslist->pagination_links = $links->render();
			
			$this->request->response =  $rawsignalslist->render();
		}
				
		public function action_details(){
			$service = Helper_Service::factory("rawsignal");
			$signal = $service->get($_POST['id']);
			$segment = $service->getSamples($signal->id);

			$page_view = View::factory("page");
			$page_view->page_title = "Detalles Registro Original";
						
			$signal_view = View::factory("components/rawsignal-details");
			$signal_view->sampling_period = $signal->sampling_period->k_value;
			$signal_view->num_samples = $segment->length;
			$signal_view->min_value = $segment->min;
			$signal_view->max_value = $segment->max;
			
			$page_view->page_content = $signal_view->render();
			
			$page_view->scripts = array(
									'media/js/jqplot/jquery.jqplot.min.js',
									'media/js/jqplot/plugins/jqplot.canvasTextRenderer.min.js',
									'media/js/jqplot/plugins/jqplot.canvasAxisLabelRenderer.min.js',
									'media/js/jqplot/plugins/jqplot.canvasAxisTickRenderer.min.js',
									'media/js/jqplot/plugins/jqplot.highlighter.min.js',
									'media/js/jqplot/plugins/jqplot.cursor.min.js',
									'media/js/jqplot/plugins/jqplot.dateAxisRenderer.min.js',
									'media/js/js_controls/js_chart_settings.js',
									'media/js/js_controls/js_chart_series.js',
									'media/js/js_controls/js_chart.js');
			
			$user_script = View::factory('js-user-scripts/raw-signal');
			$user_script->chart_data = $segment->toJSON();
				
			$page_view->user_script = $user_script->render();
									
			$this->request->response = $page_view->render();
		}
		
		public function action_data(){
			$id = $_GET['id'];
			$service = Helper_Service::factory("rawsignal");
			
			$segment = $service->getSamples($id);
			
			header('Cache-Control: no-cache, must-revalidate');
			header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
			header('Content-type: application/json');
			$this->request->response = $segment->toJSON();
		}
		
	}
